<?php
namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\validate\AdminUser as ValidateAdminUser;
use app\common\exception\SuccessException;
use app\service\api\AdminUserService;
use think\Request;

/**
 * 用户
 */
class AdminUser extends BaseController
{
    /**
     * 列表
     * @param Request $request
     */
    public function index(Request $request, AdminUserService $adminUserService)
    {
        $list = $adminUserService->lists($request);
        throw new SuccessException(['content' => $list]);
    }

    /**
     * 添加管理员
     */
    public function save(AdminUserService $adminUserService, ValidateAdminUser $validateAdminUser)
    {
        $data = $validateAdminUser->scene('save')->goCheck();
        $data['permission_ids'] = $validateAdminUser->getIds('role_ids');
        $res = $adminUserService->createUser($data);

        throw new SuccessException([
            'error_code' => $res['error'],
            'msg'        => $res['message'],
            'content'    => $res['content']
        ]);
    }

    /**
     * 更新管理员
     */
    public function update($id, AdminUserService $service, ValidateAdminUser $validate)
    {
        $data = $validate->scene('update')->goCheck();
        $data['role_ids'] = $validate->getIds('role_ids');
        $res = $service->updateUser($id, $data);

        throw new SuccessException([
            'error_code' => $res['error'],
            'msg'        => $res['message'],
            'content'    => $res['content']
        ]);
    }

    /**
     * 删除管理员
     * @param $id
     */
    public function delete($id, AdminUserService $service, ValidateAdminUser $validate)
    {
        $validate->scene('id')->goCheck();
        $res = $service->delUser(intval($id));

        throw new SuccessException([
            'error_code' => $res['error'],
            'msg'        => $res['message'],
            'content'    => $res['content']
        ]);
    }

    /**
     * 获取管理员信息
     * @param $id
     */
    public function read($id, AdminUserService $service, ValidateAdminUser $validate)
    {
        $validate->scene('id')->goCheck();
        $res = $service->getUser($id);
        throw new SuccessException([
            'error_code' => $res['error'],
            'msg'        => $res['message'],
            'content'    => $res['content']
        ]);
    }
}
